package com.bojiu.lawsearch.lawsearch.controller;

import com.bojiu.lawsearch.lawsearch.model.Page;
import com.bojiu.lawsearch.lawsearch.model.Profession;
import com.bojiu.lawsearch.lawsearch.service.ProfessionService;
import com.bojiu.lawsearch.lawsearch.util.*;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @author: afdiao
 * @Date：2020/3/8 22:13
 * @Description: 行业管理
 */
@Api(description = "后台行业管理接口，功能已完成！！！")
@RestController
@RequestMapping("profession")
public class BackProfessionController {

    @Autowired
    AuthorizationHandle authorizationHandle;

    @Autowired
    ProfessionService professionService;

    /**
     * 获取行业列表
     *
     * @param pageNum
     * @param pageSize
     * @param request
     * @return
     */
    @ApiOperation(value = "行业列表", notes = "后台获取行业列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = Profession.class),
            @ApiResponse(code = 600, message = "未检测到登录，跳转重新登录"),
            @ApiResponse(code = 500, message = "没有权限访问该接口")
    })
    @GetMapping("list/{pageNum}/{pageSize}")
    public JsonResult getList(@ApiParam(name = "pageNum", value = "当前页码", required = true) @PathVariable("pageNum") int pageNum, @ApiParam(name = "pageSize", value = "每页个数", required = true) @PathVariable("pageSize") int pageSize, HttpServletRequest request) {
        Page<Profession> list = professionService.getList(pageNum, pageSize);
        return new JsonResult(ResultCode.SUCCESS, list);
    }

    /**
     * 更新行业
     *
     * @param id
     * @param string
     * @param request
     * @return
     */
    @ApiOperation(value = "更新行业", notes = "后台更新行业")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = JsonResult.class),
            @ApiResponse(code = 600, message = "未检测到登录，跳转重新登录"),
            @ApiResponse(code = 500, message = "没有权限访问该接口"),
            @ApiResponse(code = 1200, message = "请求字段为空"),
            @ApiResponse(code = 1800, message = "行业名已存在")
    })
    @PostMapping("update/{id}")
    public JsonResult updateProfession(@ApiParam(name = "id", value = "需要更新的行业id", required = true) @PathVariable("id") int id, @ApiParam(name = "name", value = "需要更新的行业名，json格式", required = true) @RequestBody String string, HttpServletRequest request) {
        //先判断权限
        JsonResult authon = authorizationHandle.backHandler(request, Constances.ALL);
        if (!authon.getCode().equals("200")) {
            return authon;
        }
        Gson gson = new Gson();
        JsonObject jsonObject = gson.fromJson(string, JsonObject.class);
        String name = jsonObject.get("name").getAsString();
        professionService.updateProfession(id, name);
        return new JsonResult(ResultCode.SUCCESS);
    }

    /**
     * 录入新行业
     *
     * @param profession
     * @param errors
     * @param request
     * @return
     */
    @ApiOperation(value = "创建新行业", notes = "后台创建新行业")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = JsonResult.class),
            @ApiResponse(code = 600, message = "未检测到登录，跳转重新登录"),
            @ApiResponse(code = 500, message = "没有权限访问该接口"),
            @ApiResponse(code = 1200, message = "请求字段为空"),
            @ApiResponse(code = 1800, message = "行业名已存在")
    })
    @PostMapping("insert")
    public JsonResult insert(@ApiParam(name = "行业对象", value = "json格式", required = true) @Valid @RequestBody Profession profession, Errors errors, HttpServletRequest request) {
        //先判断权限
        JsonResult authon = authorizationHandle.backHandler(request, Constances.ALL);
        if (!authon.getCode().equals("200")) {
            return authon;
        }

        if (errors.hasErrors()) {
            JsonResult jsonResult = UtilTools.handleNullField(errors);
            return jsonResult;
        }
        professionService.insert(profession);
        return new JsonResult(ResultCode.SUCCESS);
    }

//    /**
//     * 删除相关行业
//     * @param id
//     * @param request
//     * @return
//     */
//    @ApiOperation(value = "删除行业", notes = "后台删除行业")
//    @ApiResponses({
//            @ApiResponse(code = 200, message = "成功", response = Setting.class),
//            @ApiResponse(code = 600, message = "未检测到登录，跳转重新登录"),
//            @ApiResponse(code = 500, message = "没有权限访问该接口"),
//            @ApiResponse(code = 1200, message = "请求字段为空"),
//            @ApiResponse(code = 1800, message = "行业名已存在")
//    })
//    @GetMapping("delete/{id}")
//    public String delete(@PathVariable("id") int id, HttpServletRequest request) {
//        //先判断权限
//        JsonResult authon = authorizationHandle.backHandler(request, Constances.ALL);
//        if (!authon.getCode().equals("200")) {
//            return new Gson().toJson(authon);
//        }
//        professionService.delete(id);
//        return new Gson().toJson(new JsonResult(ResultCode.SUCCESS));
//    }
}
